Tired of the unusually warm winter the
residents of Sudislavl decided to find out whether this is a long thaw in the
history of weather observations. They appealed to the forecasters, and those,
in turn, do research statistics for previous years. They are interested in how
many days it lasted the longest thaw.
The thaw is a period when the daily average
temperature exceeds 0 degrees Celsius. Write a program that helps
forecasters to work.
Input. The first line contains the number of observed days n (1 ≤ n ≤ 1000). The next line contains n integers –
the average temperature on the corresponding day. Temperatures are integers in
the range from -50 to 50.
Output. Print one number – the length of the longest thaw,
which is the largest number of consecutive days during which the average daily
temperature exceeded 0 degrees. If each day the temperature was
non-positive, output 0.
Sample
input |
Sample
output |
6 -20 30 0 50 10 -10 |
2 |
arrays
In the variable res find the length of the longest thaw.
In the variable temp find the length of the current thaw (the number of consecutive
days with positive temperatures).
Process the temperature
measurements every day. If it is positive, then increase the length of the
current thaw temp by 1. Otherwise, reset its value to 0. Among all
possible values of temp, find the
maximum in the variable res.
Example
Consider the sample.
Read
the number of days n.
scanf("%d", &n);
In the variable res find the length of the longest thaw.
In the variable temp find the length of the current thaw (the number of days with
positive temperature).
temp = res = 0;
Process the temperatures for n days.
for (i = 0; i < n; i++)
{
scanf("%d", &cur);
If cur > 0, then increase
the length of the current thaw by 1. Otherwise, reset its value to 0.
if (cur > 0) temp++; else temp = 0;
Among all the lengths of the current
thaw, find the maximum.
if (temp > res) res = temp;
}
Print the answer.
printf("%d\n", res);